package cn.schoolwow.workflow.test.module.oa.common.flow.node;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.workflow.domain.deploy.*;
import cn.schoolwow.workflow.test.module.oa.common.flow.common.ConvertWorkFlowDeployJumpCommonFlow;
import cn.schoolwow.workflow.test.module.oa.common.flow.step.LoopConvertChildNodeCommonFlow;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class ConvertExclusiveGatewayCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        addStartGatewayNode(flowContext);
        addEndGatewayNode(flowContext);
        addConditionNodes(flowContext);
    }

    @Override
    public String name() {
        return "转换排他网关";
    }

    private void addStartGatewayNode(FlowContext flowContext){
        WorkFlowDeployNode workFlowDeployNode = flowContext.checkInstanceData(WorkFlowDeployNode.class);
        WorkFlowDeployFileContext workFlowDeployFileContext = flowContext.checkInstanceData(WorkFlowDeployFileContext.class);

        //添加排他网关开始节点
        WorkFlowDeployNode startNode = new WorkFlowDeployNode();
        startNode.name = workFlowDeployNode.name+"开始";
        startNode.type = DeployNodeType.ExclusiveGateway.value;
        startNode.gatewayPosition = DeployGatewayPositionType.Start.value;
        workFlowDeployFileContext.nodes.add(startNode);

        flowContext.putCurrentFlowData("startNode", startNode);
    }

    private void addEndGatewayNode(FlowContext flowContext){
        WorkFlowDeployNode workFlowDeployNode = flowContext.checkInstanceData(WorkFlowDeployNode.class);
        WorkFlowDeployFileContext workFlowDeployFileContext = flowContext.checkInstanceData(WorkFlowDeployFileContext.class);

        //添加排他网关结束节点
        WorkFlowDeployNode endNode = new WorkFlowDeployNode();
        endNode.name = workFlowDeployNode.name+"结束";
        endNode.type = DeployNodeType.ExclusiveGateway.value;
        endNode.gatewayPosition = DeployGatewayPositionType.End.value;
        workFlowDeployFileContext.nodes.add(endNode);

        flowContext.putCurrentFlowData("endNode", endNode);
    }

    private void addConditionNodes(FlowContext flowContext){
        JSONObject childNode = flowContext.checkData("childNode", JSONObject.class);
        WorkFlowDeployNode startNode = flowContext.checkData("startNode", WorkFlowDeployNode.class);
        WorkFlowDeployNode endNode = flowContext.checkData("endNode", WorkFlowDeployNode.class);

        JSONArray conditionNodes = childNode.getJSONArray("conditionNodes");
        for(int i=0;i<conditionNodes.size();i++){
            JSONObject conditionNode = conditionNodes.getJSONObject(i);

            List<List<WorkFlowDeployJumpFilterCondition>> filterCondition = getFilterCondition(conditionNode);

            //默认条件
            if(null==conditionNode.get("childNode")){
                //添加网关开始节点和当前节点
                flowContext.startFlow(new ConvertWorkFlowDeployJumpCommonFlow())
                        .putCurrentCompositeFlowData("currentNode", startNode)
                        .putCurrentCompositeFlowData("nextNode", endNode)
                        .putCurrentCompositeFlowData("filterCondition", filterCondition)
                        .putCurrentCompositeFlowData("priorityLevel", conditionNode.getIntValue("priorityLevel"))
                        .putCurrentCompositeFlowData("jumpName", conditionNode.getString("name"))
                        .execute();
                continue;
            }

            //递归处理当前节点的子节点
            {
                JSONObject currentConditionChildNode = conditionNode.getJSONObject("childNode");
                flowContext.startFlow(new LoopConvertChildNodeCommonFlow())
                        .putCurrentCompositeFlowData("topNode", currentConditionChildNode)
                        .execute();
                //添加网关开始节点和当前节点
                flowContext.startFlow(new ConvertWorkFlowDeployJumpCommonFlow())
                        .putCurrentCompositeFlowData("currentNode", startNode)
                        .putCurrentCompositeFlowData("nextChildNode", currentConditionChildNode)
                        .putCurrentCompositeFlowData("filterCondition", filterCondition)
                        .putCurrentCompositeFlowData("priorityLevel", conditionNode.getIntValue("priorityLevel"))
                        .putCurrentCompositeFlowData("jumpName", conditionNode.getString("name"))
                        .execute();
            }
            {
                JSONObject currentChildNode = flowContext.checkData("currentChildNode", JSONObject.class);
                //添加当前节点和网关结束节点
                flowContext.startFlow(new ConvertWorkFlowDeployJumpCommonFlow())
                        .putCurrentCompositeFlowData("currentChildNode", currentChildNode)
                        .putCurrentCompositeFlowData("nextNode", endNode)
                        .execute();
            }
        }
    }

    private List<List<WorkFlowDeployJumpFilterCondition>> getFilterCondition(JSONObject conditionNode){
        JSONArray conditionGroups = conditionNode.getJSONArray("conditionGroups");
        if(null==conditionGroups||conditionGroups.isEmpty()){
            return null;
        }

        List<List<WorkFlowDeployJumpFilterCondition>> list = new ArrayList<>();
        for(int j=0;j<conditionGroups.size();j++){
            JSONObject conditionGroup = conditionGroups.getJSONObject(j);
            //里层
            List<WorkFlowDeployJumpFilterCondition> workFlowDeployJumpFilterConditionList = new ArrayList<>();
            JSONArray conditions = conditionGroup.getJSONArray("conditions");
            for(int k=0;k<conditions.size();k++){
                JSONObject condition = conditions.getJSONObject(k);

                WorkFlowDeployJumpFilterCondition workFlowDeployJumpFilterCondition = new WorkFlowDeployJumpFilterCondition();
                workFlowDeployJumpFilterCondition.field = condition.getString("varName");
                workFlowDeployJumpFilterCondition.operator = convertOperator(condition.getIntValue("operator"));
                workFlowDeployJumpFilterCondition.value = condition.getString("val");
                workFlowDeployJumpFilterConditionList.add(workFlowDeployJumpFilterCondition);
            }
            list.add(workFlowDeployJumpFilterConditionList);
        }
        return list;
    }

    private String convertOperator(int operator){
        switch (operator){
            case 0:{return "=";}
            case 1:{return "!=";}
            case 2:{return "<";}
            case 3:{return "<=";}
            case 4:{return ">";}
            case 5:{return ">=";}
            case 10:{throw new IllegalArgumentException("暂不支持[包含于]过滤条件");}
            case 11:{throw new IllegalArgumentException("暂不支持[不包含于]过滤条件");}
            case 12:{throw new IllegalArgumentException("暂不支持[等于]过滤条件");}
            case 13:{throw new IllegalArgumentException("暂不支持[不等于]过滤条件");}
            case 14:{throw new IllegalArgumentException("暂不支持[包含]过滤条件");}
            case 15:{throw new IllegalArgumentException("暂不支持[不包含]过滤条件");}
            case 20:{throw new IllegalArgumentException("暂不支持[属于]过滤条件");}
            case 21:{throw new IllegalArgumentException("暂不支持[不属于]过滤条件");}
            default:{
                throw new IllegalArgumentException("暂不支持的过滤条件类型!值:"+operator);
            }
        }
    }
}
